package com.tt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tt.entity.pojo.MovieInfoPO;
import com.tt.entity.pojo.MovieInfoTypePO;
import com.tt.mapper.MovieInfoPOMapper;
import com.tt.mapper.MovieInfoTypePOMapper;
import com.tt.service.MovieInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tt.service.MovieInfoTypeService;
import com.tt.util.ConstUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 电影信息表 服务实现类
 * </p>
 *
 * @author TP
 * @since 2023-03-04 09:14:11
 */
@Service
public class MovieInfoServiceImpl extends ServiceImpl<MovieInfoPOMapper, MovieInfoPO> implements MovieInfoService {
    private final MovieInfoTypePOMapper movieInfoTypePOMapper;
    private final MovieInfoPOMapper movieInfoPOMapper;

    public MovieInfoServiceImpl(MovieInfoTypePOMapper movieInfoTypePOMapper, MovieInfoPOMapper movieInfoPOMapper) {
        this.movieInfoTypePOMapper = movieInfoTypePOMapper;
        this.movieInfoPOMapper = movieInfoPOMapper;
    }

    @Override
    public ArrayList<MovieInfoPO> getMovieByType(String type, Integer current) {
        ArrayList<MovieInfoPO> vo = new ArrayList<>();

        // movieIds
        List<MovieInfoTypePO> movies = movieInfoTypePOMapper.selectList(new QueryWrapper<MovieInfoTypePO>().eq("type", type));
        if (movies == null) {
            return null;
        }
        List<Long> movieIds = movies.stream().map(MovieInfoTypePO::getMovieId).collect(Collectors.toList());


        Page<MovieInfoPO> page = new Page<>(current, ConstUtil.PAGE_SIZE);
        QueryWrapper<MovieInfoPO> wrapper = new QueryWrapper<>();
        wrapper.in("id", movieIds).orderByDesc("score");
        Page<MovieInfoPO> result = movieInfoPOMapper.selectPage(page, wrapper);

        return new ArrayList<>(result.getRecords());
    }
}
